package com.wswy.wzcx.ui.data;

import android.text.TextUtils;
import com.wswy.wzcx.model.pay.PayStyleModel;
import java.util.Map;

public class PayResult {

	private boolean paySuccess = false; //支付成功或失败
	private boolean isCancel = false;  //是否取消支付

	private int payStyle;  //支付方式
	private String msg;   //错误消息



	public static PayResult buildFromAlipay(Map<String, String> rawResult){
		return new PayResult(rawResult);
	}

	public static PayResult buildFromAlipay(String resultCode){
		return new PayResult(resultCode);
	}

	public static PayResult buildFromWechat(int errCode){
		return new PayResult(errCode);
	}


	private PayResult(Map<String, String> rawResult) {

		payStyle = PayStyleModel.ALIPAY.getCode();

		if (rawResult == null) {

			msg = "支付失败";

			return;
		}

		// https://docs.open.alipay.com/204/105301/
		String resultStatus =rawResult.get("resultStatus");

		initAlipay(resultStatus);

	}

	private PayResult(String resultStatus){
		payStyle = PayStyleModel.ALIPAY.getCode();

		if (resultStatus == null) {

			msg = "支付失败";

			return;
		}
		initAlipay(resultStatus);
	}

	private  void  initAlipay(String resultStatus){
		if (TextUtils.equals(resultStatus, "9000")) {
			// 该笔订单是否真实支付成功，需要依赖服务端的异步通知。
			paySuccess = true;
			msg = "支付成功";

		} else {
			//6001 取消 6002网络连接出错
			if (TextUtils.equals(resultStatus,"6001")){
				//取消
				paySuccess = false;
				isCancel = true;
				msg = "取消支付";
			}else if (TextUtils.equals(resultStatus,"6002")){
				msg = "支付失败，网络连接出错";
			}else if (TextUtils.equals(resultStatus,"4000")){
				msg = "订单支付失败，请稍后重试";
			}else if (TextUtils.equals(resultStatus,"8000")){
				msg = "正在处理中，请稍后。。";
			}else if (TextUtils.equals(resultStatus,"5000")){
				msg = "重复请求，请返回重试。";
			}else{
				msg = "支付失败";
			}
		}
	}


	/**
	 * 微信支付
	 * @param errCode
	 */
	private PayResult(int errCode){

		payStyle = PayStyleModel.WECHAT.getCode();

		if (errCode == 0){
			paySuccess = true;
			msg = "支付成功";
		}else if (errCode == -2){

			paySuccess = false;
			isCancel = true;

			msg = "取消支付";
		}else{
			//支付失败
			msg = "支付失败";
		}
	}


	public boolean isPaySuccess() {
		return paySuccess;
	}

	public boolean isCancel() {
		return isCancel;
	}

	public int getPayStyle() {
		return payStyle;
	}

	public String getMsg() {
		return msg;
	}
}
